package co.baiku.boot.common.tools;

import java.math.BigInteger;

public class HexTools {

    /**
     * 10 -> 16
     *
     * @param number 数字
     * @param low    是否低位在前,默认高位在前
     * @return
     */
    public static String encodeHEX(Integer number, boolean low) {
        String hexString = Integer.toHexString(number);
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }
        if (hexString.length() / 2 % 2 != 0) {
            hexString = "00" + hexString;
        }
        if (low) {
            hexString = highToLow(hexString);
        }
        return hexString.trim();
    }

    public static String encodeIntegers(Integer... number) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer integer : number) {
            String hexString = Integer.toHexString(integer);
            if (hexString.length() % 2 != 0) {
                hexString = "0" + hexString;
            }
            stringBuilder.append(hexString);
        }
        return stringBuilder.toString();
    }

    /**
     * 16 -> 10
     *
     * @param hexString 16进制字符串
     * @param low       是否低位在前,默认高位在前
     * @return
     */
    public static int decodeHEX(String hexString, boolean low) {
        if (low) {
            hexString = highToLow(hexString);
        }
        BigInteger bigint = new BigInteger(hexString, 16);
        return bigint.intValue();
    }


    /**
     * 高位转低位
     *
     * @param hexString
     * @return
     */
    public static String highToLow(String hexString) {
        char[] chars = hexString.toCharArray();
        String[] hexArr = new String[hexString.length() / 2];
        String arrItem = null;
        for (int i = 0; i < chars.length; i++) {
            if (i % 2 != 0) {
                hexArr[hexString.length() / 2 - 1 - i / 2] = arrItem + chars[i];
                arrItem = "";
            } else {
                arrItem = chars[i] + "";
            }
        }
        return String.join("", hexArr);
    }

    /**
     * hex字符串转字节数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
        String hexFormat = "0123456789ABCDEF";
        if (StringTools.isBlank(hexString)) {
            return null;
        }
        hexString = hexString.trim().replaceAll("\\s*", ""); // 去除字符串中的空格
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (hexFormat.indexOf(hexChars[pos]) << 4 | hexFormat
                    .indexOf(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * 字节转hex字符串
     *
     * @param byteArray
     * @return
     */
    public static String bytesToHexString(byte[] byteArray) {
        StringBuilder result = new StringBuilder();
        String hex;
        for (int i = 0; i < byteArray.length; i++) {
            hex = Integer.toHexString(byteArray[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            result.append(hex.toUpperCase());
        }
        return result.toString();
    }

    /**
     * 求和校验
     *
     * @param data
     * @return
     */
    public static String checksum(String data) {
        if (StringTools.isBlank(data)) {
            return "";
        }
        int total = 0;
        int len = data.length();
        int num = 0;
        while (num < len) {
            String s = data.substring(num, num + 2);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        //用256求余最大是255，即16进制的FF
        int mod = total % 256;
        String hex = Integer.toHexString(mod);
        len = hex.length();
        // 如果不够校验位的长度，补0,这里用的是两位校验
        if (len < 2) {
            hex = "0" + hex;
        }
        return hex;
    }

}
